home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.09 Sep 90 / Debugger DA / Round Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-26  |  9.2 KB  |  361 lines  |  [TEXT/KAHL]

  1. /********************************************************************/
  2. /*                                                                    */
  3. /*    Source  - Round Window.c                                        */
  4. /*    Author  - Alexander S. Colwell, Copyright (C) 1988, 1989        */
  5. /*                                                                    */
  6. /*    Purpose - This will display a round window using "Debugger" DA    */
  7. /*              demostration.                                            */
  8. /*                                                                    */
  9. /*    Routine - Main              : Main entry point module.            */
  10. /*              DoOpen            : Open the DA and initialized.        */
  11. /*            DoClose           : Close the DA and wrap it up.        */
  12. /*              DoControl        : Process DA control commands.        */
  13. /*              DoEvent          : Process DA event.                    */
  14. /*              DoUpdate           : Update window.                        */
  15. /*              DoMouse           : Process mouse-down.                */
  16. /*              DoKey               : Process key inputs.                */
  17. /*              DoContent           : Process mouse down inside window.    */
  18. /*              GetResourceID    : Get DA resource ID number.            */
  19. /*                                                                    */
  20. /*    Revisions - None.                                                */
  21. /*                                                                    */
  22. /********************************************************************/
  23.  
  24. #include <Color.h>                    /* Color Manager defs            */
  25. #include <math.h>                    /* Math defs                    */
  26. #include <SetUpA4.h>                /* Desk Accessory defs            */
  27. #include <VRetraceMgr.h>            /* Vertical Retrace Manager defs*/
  28. #include "Debugger.h"                /* Debugger defs                */
  29.  
  30.                                     /* Misc definitions                */
  31. #define    NIL        (0L)                /* NIL pointer                    */
  32. #define    abs(a)    (a<0?-a:a)            /* Absolute macro function        */
  33. #define    min(a,b) (a<b?a:b)            /* Minumim macro function        */
  34. #define    max(a,b) (a<b?b:a)            /* Maximum macro function        */
  35.  
  36.                                     /* Resource ID #'s                */
  37. #define    windID            0            /* Window ID #                    */
  38.  
  39. #define    helloWorld        "\pHello, World"/* String message            */
  40.  
  41. typedef struct {                    /* VBL data structure            */
  42.     VBLTask    vblTask;                /* VBL                            */
  43.     DBGHDL    dbgHdl;                    /* Debugger handler                */
  44.     short    counter;                /* Tick counter                    */
  45.     } VBL;
  46. typedef VBL    *VBLPTR;
  47.  
  48. DCtlPtr         dce;                /* Device control entry          */
  49. WindowPtr         wp = NIL;            /* My window pointer             */
  50. short             alreadyOpen = FALSE;/* 1 -> DA is already open      */
  51. VBLPTR            vblPtr = NIL;        /* VBL data block                */
  52. Handle            vblHdl = NIL;        /* VBL code handle                */
  53. DBGHDL            dbgHdl = NIL;        /* Debugger reference handle    */ 
  54.  
  55. main(p,d,n)
  56.  
  57.     cntrlParam     *p;                    /* Parameter block              */
  58.     DCtlPtr     d;                    /* Device control entry          */
  59.     short         n;                    /* Entry point selector          */
  60.     
  61.     {
  62.  
  63.         WindowPtr    savePort;        /* Save current window port        */
  64.  
  65.         if (d->dCtlStorage == 0) {    /* Check if got the globals 1st!*/
  66.         
  67.             if (n == 0)    {            /* Check if requesting "Open"    */
  68.             
  69.                 SysBeep(1);            /* Beep da user!                */
  70.                 
  71.                 CloseDriver(d->dCtlRefNum);/* Close the DA            */
  72.                 
  73.             }
  74.             
  75.         }
  76.         
  77.         else {                        /* Got the globals then continue*/
  78.         
  79.             GetPort(&savePort);        /* Get current window port        */
  80.             
  81.             if (wp)                    /* Check if window ptr valid    */
  82.                 SetPort(wp);        /* Set port to my window        */
  83.                 
  84.             dce = d;                /* Save DCE ptr into our globals*/
  85.             
  86.             dce->dCtlFlags &= ~dCtlEnable;/* Set it not to be re-entrant*/
  87.             
  88.             switch(n) {                /* Handle request:                 */
  89.             
  90.                 case 0:                /*      "Open"                    */
  91.                     DoOpen(); break;    
  92.                     
  93.                 case 2:                /*      "Control"                 */
  94.                     DoControl(p->csCode,p->csParam); break;    
  95.                     
  96.                 case 4:                /*      "Close"                       */
  97.                     DoClose(); break;
  98.                     
  99.             }
  100.             
  101.             dce->dCtlFlags |= dCtlEnable;/* Enable calls once more    */
  102.             
  103.             SetPort(savePort);        /* OK, let's restore it            */
  104.             
  105.         }
  106.         
  107.         return(0);                    /* Return default success          */
  108.         
  109.     }
  110.  
  111. DoOpen()
  112.  
  113.     {
  114.     
  115.         Handle    wDEFHdl;            /* Working window def handle    */
  116.         
  117.                                     /* Add neccesary driver flags    */
  118.         dce->dCtlFlags |= dNeedLock|dNeedGoodBye;
  119.         
  120.         if (wp)                        /* Check if there a window        */
  121.             SelectWindow(wp);        /* Bring our window front         */
  122.             
  123.         if (!alreadyOpen) {            /* Check if require inits        */
  124.         
  125.             wp = GetNewWindow(GetResourceID(windID),NIL,-1L);
  126.             
  127.             if (wp) {                /* Check if got the window        */
  128.                     
  129.                                     /* Check if got our WDEF        */
  130.                 if (wDEFHdl = GetResource('WDEF',GetResourceID(0)))
  131.                     ((WindowPeek)(wp))->windowDefProc = wDEFHdl;
  132.                 
  133.                 ShowWindow(wp);        /* Show the window now            */
  134.                 
  135.                 SetPort(wp);        /* Set port to our window        */
  136.                 
  137.                 alreadyOpen = TRUE;    /* Set open DA indicator        */
  138.                 
  139.                                     /* Save window association        */
  140.                 ((WindowPeek)wp)->windowKind = dce->dCtlRefNum;
  141.                 
  142.                 dce->dCtlWindow = wp;/* Tell device where I am        */
  143.                 
  144.                 dbgHdl = DbgGetRefHdl();/* Get reference handle        */
  145.             
  146.                                     /* Check if got VBL data pointer*/
  147.                 if (vblPtr = (VBLPTR)(NewPtr((long)sizeof(VBL)))) {
  148.                 
  149.                                     /* Check if got VBL code        */
  150.                     if (vblHdl = GetResource('VBLC',GetResourceID(0))) {
  151.                     
  152.                         HLock(vblHdl);/* Lock it down for VBL task    */
  153.                         
  154.                                     /* Init VBL task                */
  155.                         vblPtr->vblTask.vblAddr = (ProcPtr)(*vblHdl);
  156.                         vblPtr->vblTask.vblCount = 120;
  157.                         vblPtr->vblTask.vblPhase = 0;
  158.                         vblPtr->vblTask.qType = vType;
  159.                         vblPtr->dbgHdl = dbgHdl;
  160.                         vblPtr->counter = 120;
  161.                         
  162.                         VInstall(vblPtr);/* Install the VBL task    */
  163.                         
  164.                     }
  165.                     
  166.                 }
  167.                 
  168.             }
  169.             
  170.             else {                    /* Fail to open                    */
  171.             
  172.                 SysBeep(1);            /* Beep da user!                */
  173.                 
  174.                 CloseDriver(dce->dCtlRefNum);/* OK, let's close DA    */
  175.                 
  176.             }
  177.             
  178.         }
  179.         
  180.     }
  181.  
  182. DoClose()
  183.  
  184.     {
  185.     
  186.         if (wp)                        /* Check if window ptr valid    */
  187.             DisposeWindow(wp);        /* Delete the window now        */
  188.         wp = NIL;                    /* Invalidate it now            */
  189.                     
  190.         if (vblPtr && vblHdl)        /* Check if VBL task is installed*/
  191.             VRemove(vblPtr);
  192.             
  193.         if (vblPtr)                    /* Check if has VBL data pointer*/
  194.             DisposPtr(vblPtr);        /* Release it now                */
  195.         vblPtr = NIL;                /* Invalidate it now            */
  196.         
  197.         if (vblHdl)    {                /* Check if has VBL code handle    */
  198.             HUnlock(vblHdl);        /* OK, it's safe to unlock it    */
  199.             ReleaseResource(vblHdl);/* Release it now                */
  200.         }
  201.         vblHdl = NIL;                /* Invalidate it now            */
  202.         
  203.         alreadyOpen = FALSE;        /* Reset it (really doesn't matter)*/
  204.         
  205.     }
  206.  
  207. DoControl(code,parm)
  208.  
  209.     short code;                        /* Control command code            */
  210.     short *parm;                    /* "csParam" list pointer        */
  211.     
  212.     {
  213.     
  214.         switch(code) {                /* Handle request:                */
  215.         
  216.             case accEvent:             /*        "Event"                    */
  217.                 DoEvent(*((EventRecord **)parm)); break;
  218.                 
  219.             case goodBye:              /*         "GoodBye"                */
  220.                 DoClose(); break;
  221.  
  222.         }
  223.         
  224.     }
  225.  
  226. DoEvent(e)
  227.  
  228.     register EventRecord *e;        /* Event Record pointer            */
  229.  
  230.     {
  231.     
  232.                                     /* Output DA's event type        */
  233.         DbgPrint(dbgHdl,"DA Event: what - %d\n",e->what);
  234.         
  235.         switch(e->what) {            /* Handle request:                */
  236.         
  237.             case updateEvt:         /*        "Update"                */
  238.                 DoUpdate();    break;
  239.                 
  240.             case mouseDown:            /*         "Mouse Down"            */
  241.                 DoMouse(e->where,e->modifiers); break;
  242.                 
  243.             case keyDown:             /*        "Key Down"                */
  244.             case autoKey:            /*        "Auto-Key Down"            */
  245.             
  246.                                     /* Handle the input key            */
  247.                 DoKey((char)(e->message & charCodeMask),
  248.                       (char)((e->message & keyCodeMask) >> 8L),
  249.                       e->modifiers);
  250.                     
  251.         }
  252.         
  253.     }
  254.  
  255. DoUpdate()
  256.  
  257.     {
  258.     
  259.         BeginUpdate(wp);            /* Start update processing        */
  260.  
  261.         EraseRect(&wp->portRect);    /* Clear the window                */
  262.         
  263.         MoveTo(abs(wp->portRect.right - wp->portRect.left) / 2 -
  264.                StringWidth(helloWorld) / 2,
  265.                abs(wp->portRect.bottom - wp->portRect.top) / 2);
  266.                
  267.         DrawString(helloWorld);        /* Draw the string message        */
  268.         
  269.         EndUpdate(wp);                /* Wrapup update processing        */
  270.  
  271.     }
  272.     
  273. DoMouse(p,modifiers)
  274.  
  275.     Point    p;                        /* Mouse point position            */
  276.     short    modifiers;                /* Mouse's modifiers            */
  277.  
  278.     {
  279.     
  280.         register long    i;            /* Working index                */
  281.         Rect            wRect;        /* Working window rect area        */
  282.         register long    wGrow;        /* Working grow size            */
  283.         ProcPtr         wDef;        /* Working window proc hdl        */
  284.     
  285.                                     /* Load it into memory            */
  286.         LoadResource(((WindowPeek)wp)->windowDefProc);
  287.         
  288.         HLock(((WindowPeek)wp)->windowDefProc);/* Lock it down        */
  289.         
  290.                                     /* Get window proc definition    */
  291.         wDef = (ProcPtr)*((WindowPeek)wp)->windowDefProc;
  292.  
  293.                                     /* Check if in "Content"        */
  294.         if (CallPascalL(8,wp,(int)wHit,p,wDef) == wInContent)
  295.             DoContent(p,modifiers);
  296.  
  297.         HUnlock(((WindowPeek)wp)->windowDefProc);/* OK, unlock it down*/
  298.         
  299.     }
  300.     
  301. DoKey(c,code,modifiers)
  302.  
  303.     char    c;                        /* Input character                */
  304.     char    code;                    /* Input code                    */
  305.     short    modifiers;                /* Input modifiers                */
  306.     
  307.     {
  308.     
  309.         if (modifiers & cmdKey) {    /* Check if key command            */
  310.         
  311.             if (c == 'q')            /* Check if time to quit        */
  312.                 CloseDriver(dce->dCtlRefNum);/* OK, let's close DA    */
  313.                 
  314.             else                    /* Opps, invalid command        */
  315.             SysBeep(1);                /* Let the user know about it    */
  316.                     
  317.         }
  318.         
  319.     }
  320.  
  321. DoContent(p,modifiers)
  322.  
  323.     Point     p;                        /* Mouse down point                */
  324.     short    modifiers;                /* Mouse's modifiers            */
  325.     
  326.     {
  327.     
  328.         Rect            wRect;        /* Working window rect area        */
  329.         register long    wGrow;        /* Working grow point            */
  330.         WindowPtr        wPtr;        /* Working window pointer        */
  331.         
  332.                                     /* Check if want to resize it    */
  333.         if (modifiers & (shiftKey | optionKey | cmdKey)) {
  334.         
  335.             SetRect(&wRect,50,50,32767,32767);/* Define the limits    */
  336.             
  337.             wGrow = GrowWindow(wp,p,&wRect);/* OK, let's grow it now*/
  338.             
  339.             if (wGrow) {            /* Check if specified new size    */
  340.             
  341.                                     /* Reset new size                */
  342.                 SizeWindow(wp,LoWord(wGrow),HiWord(wGrow),TRUE);
  343.                 
  344.                 InvalRect(&wp->portRect);/* Invalidate everything    */
  345.                 
  346.             }
  347.             
  348.         }
  349.         
  350.         else {                        /* Nope, just move it            */
  351.         
  352.                GetWMgrPort(&wPtr);        /* Get window manager pointer    */
  353.                
  354.             DragWindow(wp,p,&wPtr->portRect);/* Drag the window        */
  355.             
  356.         }
  357.         
  358.     }
  359.     
  360. GetResourceID(n) { return(0xC000 + ((~(dce->dCtlRefNum))<<5) + n); }
  361.